package tool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static int intervalDay(String startTime, String endTime) {
		int intervalDays = 0;
		Calendar tmp = Calendar.getInstance();
		tmp.setTime(str2Date(startTime));
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(str2Date(endTime));
		while (tmp.before(endCal) == true) {
			intervalDays++;
			tmp.setTime(DateUtils.str2Date(addDay(startTime, intervalDays)));
		}
		return intervalDays;
	}

	public static String addDay(String day, int x) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, x);// 24小时制
		date = cal.getTime();
		cal = null;
		return format.format(date);
	}

	public static String addDay(Date date, int x) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, x);// 24小时制
		date = cal.getTime();
		cal = null;
		return format.format(date);
	}

	public static String addHour(String day, int x) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, x);// 24小时制
		date = cal.getTime();
		cal = null;
		return format.format(date);
	}

	public static Date str2Date(String strDate) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			date = sdf.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;

	}

	public static String date2Str(Date date, String separator) {
		return String.valueOf((date.getYear() + 1900)) + separator + String.valueOf((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : ("0" + (date.getMonth() + 1))) + separator
				+ String.valueOf(date.getDate() > 9 ? date.getDate() : "0" + date.getDate()) + " " + String.valueOf(date.getHours() > 9 ? date.getHours() : "0" + date.getHours()) + ":"
				+ String.valueOf(date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes()) + ":" + String.valueOf(date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds());
	}

	public static String date2DateStr(Date date, String separator) {
		return String.valueOf((date.getYear() + 1900)) + separator + String.valueOf((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : ("0" + (date.getMonth() + 1))) + separator
				+ String.valueOf(date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
	}

	/**
	 * compute intervals between time stamps
	 * 
	 * @param dateTimes
	 * @return
	 */
	public static ArrayList<Integer> getIntervals(ArrayList<Date> dateTimes) {
		ArrayList<Integer> intervals = new ArrayList<Integer>();
		for (int i = 0; i < dateTimes.size() - 1; i++) {
			Date cur = dateTimes.get(i);
			Date next = dateTimes.get(i + 1);
			intervals.add(getHourDistance(next, cur));
		}
		return intervals;
	}

	/**
	 * compute the distance between two date,unit hour
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getHourDistance(Date date1, Date date2) {
		int quot = 0;
		try {
			quot = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60 / 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return quot;
	}

	/**
	 * compute the distance between two date,unit hour
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDayDistance(Date date1, Date date2) {
		int quot = 0;
		try {
			quot = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return quot;
	}

	public static void main(String args[]) {
		Date today = new Date();
		String nextDay = DateUtils.addDay(today, 1);
		System.out.println(today);
		System.out.println(nextDay);
		// DateUtils.addDay(day, x);

	}
}
